package com.debai.service.Impl;

import com.debai.mapper.OrderMapper;
import com.debai.pojo.Order;
import com.debai.pojo.Position;
import com.debai.pojo.Screen;
import com.debai.pojo.User;
import com.debai.service.OrderService;
import com.debai.service.ScreenService;
import com.debai.service.UserService;
import com.debai.util.JwtUtil;
import com.debai.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UserService userService;
    @Autowired
    private ScreenService screenService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private com.debai.service.PositionService positionService;

    @Override
    public int insert(Order order, String token, String screenPosition) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("userId");
        order.setUserId(userId);
        Screen screen = screenService.selectByName(screenPosition);
        order.setScreenId(screen.getId());

        User user = userService.selectById(userId);

        int positionId = user.getPositionId();
        order.setPositionId(positionId);

        double money = order.getMoney();
        BigDecimal bd = new BigDecimal(money);
        //调用函数 参数2表示保留两位小数，ROUND_HALF_UP表示四舍五入
        //bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bdMoney = bd.setScale(2, BigDecimal.ROUND_HALF_UP);

        //输出结果为保留两位小数
        System.out.println(bdMoney);
        double money1 = bdMoney.doubleValue();
        order.setMoney(money1);

        int i = orderMapper.insert(order);
        return i;
    }

    @Override
    public int delete(int id) {
        int i = orderMapper.delete(id);
        return i;
    }

    @Override
    public int update(Order order, String token, String screenPosition) {
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        int userId = (int) map.get("userId");
        System.out.println("screenPosition===>:" + screenPosition);
        Screen screen = screenService.selectByName(screenPosition);

        order.setScreenId(screen.getId());

        double money = order.getMoney();
        BigDecimal bigDecimal = new BigDecimal(money);
        BigDecimal bdMoney = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
        order.setMoney(bdMoney.doubleValue());
        int i = orderMapper.updata(order);

        return i;
    }

    @Override
    public List<Order> selectAllPosititionSumMoney(LocalDateTime startTime, LocalDateTime endTime) {

        List<Order> listOrder = orderMapper.selectAllPosititionSumMoney(startTime, endTime);
        listOrder.forEach(order -> {
            int positionId = order.getPositionId();
            Position position = positionService.selectById(positionId);
            order.setPosition(position);
        });
        return listOrder;
    }

    @Override
    public List<Order> selectOnePasition(int positionId, LocalDateTime startTime, LocalDateTime endTime) {
        List<Order> listOrder = orderMapper.selectOnePasition(positionId, startTime, endTime);
        return listOrder;
    }

    @Override
    public List<Order> selectUserOfPasition(String token, LocalDateTime startTime, LocalDateTime endTime) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        int userId = (int) map.get("userId");
        System.out.println(userId);
        User user = userService.selectById(userId);
        System.out.println(user.toString());
        int positionId = user.getPositionId();
        System.out.println(positionId);
        List<Order> orders = orderMapper.selectOnePasition(positionId, startTime, endTime);
        orders.forEach(
                order -> {
                    BigDecimal sumMoney = order.getSumMoney();
                    sumMoney = sumMoney.setScale(2, BigDecimal.ROUND_HALF_UP);
                    order.setSumMoney(sumMoney);
                }
        );
        return orders;
    }

    @Override
    public List<Order> detailOfPasitionOrder(int positionId, LocalDateTime startTime, LocalDateTime endTime) {
        List<Order> orders = orderMapper.detailOfPasitionOrder(positionId, startTime, endTime);
        orders.forEach(order -> {
            int userId = order.getUserId();
            User user = userService.selectById(userId);
            order.setUser(user);

            int positionId1 = order.getPositionId();
            Position position = positionService.selectById(positionId1);
            order.setPosition(position);

            int screenId = order.getScreenId();
            Screen screen = screenService.selectById(screenId);
            order.setScreen(screen);
        });
        return orders;
    }

    @Override
    public List<Order> detailUserOfPositionOrder(String token, LocalDateTime startTime, LocalDateTime endTime) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("userId");
        User user = userService.selectById(userId);
        int positionId = user.getPositionId();

        List<Order> orders = orderMapper.detailOfPasitionOrder(positionId, startTime, endTime);
        orders.forEach(order -> {
            int userId1 = order.getUserId();
            User user1 = userService.selectById(userId1);
            order.setUser(user1);

            int positionId1 = order.getPositionId();
            Position position = positionService.selectById(positionId1);
            order.setPosition(position);

            int screenId = order.getScreenId();
            Screen screen = screenService.selectById(screenId);
            order.setScreen(screen);
        });
        return orders;
    }


    @Override
    public PageBean<Order> selectOnePasitionPageBean(int pageNum, int pageSize, String token, LocalDateTime startTime, LocalDateTime endTime) {
        //1，创建PageBean对象
        PageBean<Order> orderPageBean = new PageBean<>();

        //3.调用mapper
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("userId");
        User user = userService.selectById(userId);
        int positionId = user.getPositionId();

        //2.通过pageheple 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        List<Order> orders = orderMapper.detailOfPasitionOrder(positionId, startTime, endTime);
        orders.forEach(order -> {
            int userId1 = order.getUserId();
            User user1 = userService.selectById(userId1);
            order.setUser(user1);

            int positionId1 = order.getPositionId();
            Position position = positionService.selectById(positionId1);
            order.setPosition(position);

            int screenId = order.getScreenId();
            Screen screen = screenService.selectById(screenId);
            order.setScreen(screen);
        });


       //4,page中的方法可以获得总条数，和当前页码
        PageBean<Order> orderB = (PageBean<Order>) orders;


        //5,把数据封装到pagebean中
        orderPageBean.setTotal(orderB.getTotal());
        orderPageBean.setItems(orderB.getItems());


        return orderB;
    }
}
